import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


data_1 = pd.read_excel(io='adjuncts/adjunct_1.xlsx', sheet_name="中红外", index_col=0)    # 读取附件一内容
print(data_1)

interval_1 = [[655, 900],
              [900, 1150],
              [1180, 1290],
              [1340, 1430],
              [1430, 1480],
              [1485, 1550],
              [1550, 1700],
              [1700, 1785],
              [2800, 2875],
              [2880, 3000],
              [3100, 3500]]


def peak_interval(left, right, list_row, start):  # 计算区间峰值位置 参数按照检材编号,左闭右开

    lst = list_row[left - start: right - start]          # 切割传入函数数据   list_row是一个list
    print("===== * =====")
    lst_new = np.ndarray.tolist(lst)             # 数据类型转换
    peak = max(lst_new)                           # 计算峰值
    if peak > 0:
        return [lst_new.index(peak) + left, peak]    # 返回峰值x, y轴坐标
    else:
        return [0, 0]                                # 出现较大干扰值  返回状态码


# =========预处理数据=======

def data_pretreatment(data, interval, start):

    table = []                                            # 二维列表 用来存放预处理数据
    for r in data.index:
        row = data.loc[r].values[0:]                         # 按行读取 附件 数据
        result_row = []                                        # 存放该行数据处理结果
        for i in interval:                                     # 设i变量为区间库滚动游标
            t = peak_interval(i[0], i[1], row, start)                 # t接收返回的峰值 横纵坐标
            result_row.append(t[0])
            result_row.append(t[1])
        table.append(result_row)                          # 将该行数据存入表中
    return table


tables = data_pretreatment(data_1, interval_1, 652)
print(tables)   # 检验


# =========


'''
def find_interval(data_size, num):  # 随机取部分检材找峰值区间  data_size是指数据量大小  num代表取样的个数

    for index in range(1, num):
        rand = random.randint(1, data_size)
        row = data_1.loc[rand].values[0:]

'''





'''
print("bingo")
data.plot(y='No')
print("bingo")
plt.show()
print(data)'''

#print(data)

'''
X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
C, S = np.cos(X), np.sin(X)

plt.plot(X, C)
plt.plot(X, S)

plt.show()
'''
